<?php
/**
 * 获取带参二维码链接地址
 * @param  参数 $qr_id 参数
 * @return string        二维码链接
 */
function get_qr_img($qr_id)
{
	$qr_ticket = get_qr_ticket($qr_id);
	$url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$qr_ticket.'';
	return $url;
}

/**
 * 微信带参二维码ticket
 * @param  string $access_token 微信access_token
 * @param  int $scene_id     场景id
 * @return string               ticket
 */
function get_qr_ticket($scene_id)
{
	$access_token = get_access_token();
	$url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token.'';
	$expire_seconds = 604800;
	$action_name = "QR_SCENE";
	$action_info = array(
		'scene'		=> array('scene_id' =>$scene_id)
	);
	$data =array(
		'expire_seconds' 	=> $expire_seconds,
		'action_name' 		=> "QR_SCENE",
		'action_info' 		=> array(
			'scene' =>array(
					'scene_id' =>$scene_id
				)
			)
	);
	$data = json_encode($data);
	$res = vpost($url,$data);
	$res = stripslashes($res);
	$res = json_decode($res,true);

	return $res['ticket'];
}

/**
 * 获取微信access_token
 * @return string access_token
 */
function get_access_token()
{
	$app_info = $GLOBALS['db']->getRow('SELECT * FROM '.$GLOBALS['ecs']->table('weixin_config').' WHERE id = 1');
	$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$app_info['appid'].'&secret='.$app_info['appsecret'].'';
	$token = curl_request($url);
	$token = stripslashes($token);
	$token = json_decode($token,true);
	return $token['access_token'];
}
/*get请求*/
function curl_request($url,$data='')
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	if(!empty($data)){
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

	}else{
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
	}
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

	$res = curl_exec($ch);
	curl_close($ch); 
	return $res;
}

/*post请求*/
function send_post($url, $post_data) 
{  
	$postdata = http_build_query($post_data);  
	$options = array(  
		'http' => array(  
		'method' => 'POST',  
		'header' => 'Content-type:application/x-www-form-urlencoded',  
		'content' => $postdata,  
		'timeout' => 15 * 60
		)  
	);  
	$context = stream_context_create($options);  
	$result = file_get_contents($url, false, $context);  
	return $result;  
}

function vpost($url,$data)
{ 
	$curl = curl_init(); // 启动一个CURL会话
	curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
	curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
	curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
	curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
	curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
	curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
	curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
	$tmpInfo = curl_exec($curl); // 执行操作
	if (curl_errno($curl)) {
	   echo 'Errno'.curl_error($curl);//捕抓异常
	}
	curl_close($curl); // 关闭CURL会话
	return $tmpInfo; // 返回数据
}  
?>